package mx.netbrain.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;

import mx.netbrain.dao.ProfileDao;
import mx.netbrain.model.Profile;
import mx.netbrain.model.Tree;
import mx.netbrain.util.HibernateUtil;

public class ProfileDaoImpl implements ProfileDao, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 542708185500988607L;
	private static final Logger logger = Logger.getLogger(ProfileDaoImpl.class);

	@Override
	public Profile findByPerfil(Long user, Tree tree) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		String sql;
		Query query;
		if (tree.getRepositories() == null) {
			if (tree.getRoot() == 0) {
				sql = "select treeuser.profile from TreeUser as treeuser "
						+ "where treeuser.user.id = :user and treeuser.tree.id = :tree";
				query = session.createQuery(sql);
				query.setLong("user", user);
				query.setLong("tree", tree.getId());
			} else {
				sql = "select treeuser.profile from TreeUser as treeuser "
						+ "where treeuser.user.id = :user and treeuser.tree = :tree";
				query = session.createQuery(sql);
				query.setLong("user", user);
				query.setLong("tree", tree.getRoot());
			}
		} else {
			sql = "select treeuser.profile from TreeUser as treeuser "
					+ "where treeuser.user.id = :user and treeuser.repositories = :tree";
			query = session.createQuery(sql);
			query.setLong("user", user);
			query.setLong("tree", tree.getRepositories().getId());
		}
		return (Profile) query.uniqueResult();
	}

	@Override
	public void update(Profile profile) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.merge(profile);
			session.beginTransaction().commit();
		} catch (Exception e) {
			logger.error("Update: " + e.getMessage());
			session.beginTransaction().rollback();
		}
	}

	@Override
	public Profile findById(Long id) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		return (Profile) session.load(Profile.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Profile> listAll() {
		Session session = HibernateUtil.getSessionFactory().openSession();
		return session.createQuery("from Profile").list();
	}

	@Override
	public void save(Profile profile) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.persist(profile);
			session.beginTransaction().commit();
		} catch (Exception e) {
			logger.error("Save: " + e.getMessage());
			session.beginTransaction().rollback();
		}
	}

	@Override
	public void delete(Profile profile) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.delete(profile);
			session.beginTransaction().commit();
		} catch (Exception e) {
			System.out.println("Error al actualizar perfil");
			session.beginTransaction().rollback();
		}
	}

}
